package com.roger.cmdkit.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Character.toLowerCase;
import static java.lang.Character.toUpperCase;

public class DataType {

    public static void main(String[] arg) throws ParseException {
        System.out.println(10&23);
        System.out.println(~10);
        System.out.println(10>>2);
        System.out.println(10<<2);
        System.out.println(10>>>2);
        System.out.println(10<11 ? 10:11);
        System.out.println(10);
        System.out.println(012);
        System.out.println(0xA);
        for(int i=0;i<=10;i+=2){
            System.out.println(i);
        }

        int i = 0;
        Boolean bool =true;
        float f = 13.65f/3.4f;
        String letter = "hEllO";
        String letters="JavaLoveJavaLookJavaIsJava";
        String[] array_letter = {"hEllO","Hello"};
        String[][] array2_letter={{"1","2"},{"3","4"}};
        byte b = 0x7f;//-128~127
        char ch = 'a';
        char[] array_ch = { 'H', 'e', 'l', 'l', 'o'};
        String helloString = new String(array_ch);
        char uniChar = '\u039A';
        System.out.printf("format demo: %.3f "+"%d "+"%s\n",f,array_letter.length,helloString);
        System.out.println("output: "+array2_letter[1][0]+" "+Integer.toHexString(i & 0xFF) +" "+String.valueOf(bool)+" "+ b +" "+ch+" "+uniChar);
        for(i=0;i<=letter.length();i++){
            if(i==0) System.out.print("format letter to normal: "+toUpperCase(letter.charAt(i)));
            else if(i==letter.length())System.out.print("\n");
            else System.out.print(toLowerCase(letter.charAt(i)));
        }
        //StringBuffer & StringBuilder to update string ,not create new object
        StringBuffer strBuf= new StringBuffer("Hello");//Thread safe
        StringBuilder strBui =new StringBuilder("Hello");
        System.out.println("StringBuffer: "+strBuf+","+strBuf.append("World")+" "+strBuf.delete(5,10)+" "+strBuf.reverse());
        System.out.println("StringBuilder: "+ strBui.replace(0,1,"h"));
        //date
        Date date = new Date();
        long sysTime = System.currentTimeMillis( );
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss.SSS");
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        System.out.println("date demo: "+sysTime+" "+sdf.format(1000L)+" "+ sdf.parse("2019/11/01 23:00:00.000").getTime()+ " "+month);
        //regex
        Boolean isM = Pattern.matches("^[A-z]+ \\d+(\\.\\d+\\.[4-5].*)?", "Version 1.1.41");
        System.out.println("version is match:"+isM);

        Matcher m = Pattern.compile("Java").matcher(letters);
        System.out.println("match all:"+m.matches());
        System.out.println("match at beginning:"+m.lookingAt());

        int count=0; while (m.find()) count++;
        System.out.println("1.how many \"Java\" in letters:"+(letters.length()-letters.replaceAll("Java","").length())/4);
        System.out.println("2.how many \"Java\" in letters:"+ count+" //some mistakes happen while letters begin with \"Java\",so add space to the beginning of letters to solve this problem");

        m = Pattern.compile("Java").matcher(" "+letters);
        count=0; while (m.find()) count++;
        System.out.println("3.how many \"Java\" in letters:"+ count);
    }
}
